package com.xxxx.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.crm.base.BaseService;
import com.xxxx.crm.dao.BCustomerMapper;
import com.xxxx.crm.query.BCustomerQuery;
import com.xxxx.crm.vo.BCustomer;
import com.xxxx.crm.vo.SaleChance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BCustomerService extends BaseService<BCustomer,Integer> {

    @Resource
    private BCustomerMapper bCustomerMapper;

    public Map<String, Object> queryCustomerByParams(BCustomerQuery bCustomerQuery) {
        Map<String, Object> map = new HashMap<>();
        //开启分页
        PageHelper.startPage(bCustomerQuery.getPage(), bCustomerQuery.getLimit());
        //得到分页对象
        PageInfo<BCustomer> pageInfo = new PageInfo<>(bCustomerMapper.selectByParams(bCustomerQuery));
        //设置map对象
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        //设置分页列表
        map.put("data", pageInfo.getList());
        return map;

    }

    /**
     * 生成折线图
     *
     * @return
     */
    public Map<String, Object> countBCustomerMake() {
        Map<String, Object> map = new HashMap<>();
        //查询客户列表
        List<Map<String, Object>> datalist = bCustomerMapper.countBCustomerMake();
        //折线图X轴的数据-数组
        List<String> data1 = new ArrayList<>();
        //折线图Y轴的数据-数组
        List<String> data2 = new ArrayList<>();
        //判断数组列表，循环输出数据
        if (datalist.size() > 0 && datalist != null) {
            datalist.forEach(m -> {
                //设置x轴坐标的数据
                data1.add(m.get("cluster").toString());
                //设置y轴地数据
                data2.add(m.get("total").toString());
            });
        }
        //将data1和data2设置到map中返回给前端
        map.put("data1", data1);
        map.put("data2", data2);
        return map;
    }

    /**
     * 生成饼状图
     * @return
     */
    public Map<String,Object> countBCustomerMake02(){
        Map<String,Object> map=new HashMap<>();
        //查询客户列表
        List<Map<String,Object>> datalist=bCustomerMapper.countBCustomerMake();
        //饼状图X轴的数据-数组(数组中是字符串)
        List<String> data1=new ArrayList<>();
        //饼状图Y轴的数据-数组(数组中是对象)
        List<Map<String,Object>> data2=new ArrayList<>();
        //判断数组列表，循环输出数据
        if(datalist.size()>0 && datalist!=null){
            datalist.forEach(m->{
                //设置x轴
                data1.add(m.get("cluster").toString());
                //设置y轴
                Map<String,Object> datamap=new HashMap<>();
                datamap.put("name",m.get("cluster"));
                datamap.put("value",m.get("total"));
                data2.add(datamap);
            });
        }
        //将data1和data2设置到map中返回给前端
        map.put("data1",data1);
        map.put("data2",data2);
        return map;

    }


    /**
     * 客户统计条形图
     * @return
     */
    public Map<String, Object> customerBalanceAvg() {
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> datalist= bCustomerMapper.customerBalanceAvg();
        List<String> data1=new ArrayList<>();
        List<String> data2=new ArrayList<>();
        //放数据
        if(datalist !=null && datalist.size()>0){
            datalist.forEach(m->{
                data1.add(m.get("cluster").toString());
                data2.add(m.get("balanceAvg").toString());
            });
            map.put("data1",data1);
            map.put("data2",data2);
        }
        return map;

       
    }

    public Map<String, Object> customerProductAvg() {
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> datalist= bCustomerMapper.customerProductAvg();
        List<String> data1=new ArrayList<>();
        List<String> data2=new ArrayList<>();
        //放数据
        if(datalist !=null && datalist.size()>0){
            datalist.forEach(m->{
                data1.add(m.get("cluster").toString());
                data2.add(m.get("productAvg").toString());
            });
            map.put("data1",data1);
            map.put("data2",data2);
        }
        return map;
    }


    public Map<String, Object> customersalaryAvg() {
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> datalist= bCustomerMapper.customersalaryAvg();
        List<String> data1=new ArrayList<>();
        List<String> data2=new ArrayList<>();
        //放数据
        if(datalist !=null && datalist.size()>0){
            datalist.forEach(m->{
                data1.add(m.get("cluster").toString());
                data2.add(m.get("salaryAvg").toString());
            });
            map.put("data1",data1);
            map.put("data2",data2);
        }
        return map;
    }

    public Map<String, Object> customersatisfactionAvg() {
        Map<String,Object> map=new HashMap<>();
        List<Map<String,Object>> datalist= bCustomerMapper.customersatisfactionAvg();
        List<String> data1=new ArrayList<>();
        List<String> data2=new ArrayList<>();
        //放数据
        if(datalist !=null && datalist.size()>0){
            datalist.forEach(m->{
                data1.add(m.get("cluster").toString());
                data2.add(m.get("satisfactionAvg").toString());
            });
            map.put("data1",data1);
            map.put("data2",data2);
        }
        return map;
    }

}